<?php 

class Articles extends CI_Controller {

 function __construct() {
   parent::__construct();
   $this->load->model('articles_model');
   $this->load->model('inventory_model');
   $this->load->model('movements_model');
   $this->load->database();
   $this->load->helper('url');
 }

 //Loads the users view
 function index() {
   if($this->session->userdata('logged_in')) {
     $session_data = $this->session->userdata('logged_in');
     $data['ds_usuario'] = $session_data['ds_usuario'];
     $this->load->view('articles_view', $data);
   } else {
     redirect('login', 'refresh');
   }
 }

 function returntypes() {
   //query the database
   $result = $this->articles_model->getarticlestypes();
   if($result) {
     echo json_encode($result);
   }
 }

 function returnarticletypebyid($id) {
   //query the database
   $result = $this->articles_model->getarticletypebyid($id);
   if($result) {
     echo json_encode($result);
   }
 }

 function returnarticlebyid($id) {
   //query the database
   $result = $this->articles_model->getarticlebyid($id);
   if($result) {
     echo json_encode($result);
   }
 }

function savearticletype() {
  if($this->input->post("articletypename")){
      $articletypeid = $this->input->post('articletypeid');
      $articletypename = $this->input->post('articletypename');
      //query the database
      $result = $this->articles_model->savetype($articletypeid, $articletypename);
      if($result) {
        echo json_encode(array("response" => "success"));
      } else {
        echo json_encode(array("response" => "failed"));
      }
  } else {
      echo json_encode(array("response" => "incomplete_form"));
  }
}


function savearticle() {
  if($this->input->post("articlename")&&$this->input->post("articletype")
     &&$this->input->post("articledescription")){
      if(isset($_FILES['file'])){
        $file_element_name = "file";
        $articleid = $this->input->post("articleid");
        $articlename = $this->input->post("articlename");
        $articletype = $this->input->post("articletype");
        $articledescription = $this->input->post("articledescription");
        $articlefeatures = $this->input->post("articlefeatures");
        $articlecomments = $this->input->post("articlecomments");
        
        $articleid = $this->input->post("articleid");
        $articlename = $this->input->post("articlename");
        $articletype = $this->input->post("articletype");
        $articledescription = $this->input->post("articledescription");
        $articlefeatures = $this->input->post("articlefeatures");
        $articlecomments = $this->input->post("articlecomments");

        $result = $this->articles_model->savearticle($articleid, $articlename, $articletype, $articledescription, $articlefeatures, $articlecomments);
        if($result) {
          echo json_encode(array("response" => "success"));
        } else {
          echo json_encode(array("response" => "failed"));
        }

    } else {
        echo json_encode(array("response" => "incomplete_form"));
    }
  }
}

//Saves an article by unity into the database
function loadarticlesfile() {
  if(isset($_FILES['file'])){
      $file_element_name = "file";

      $articletype = $this->input->post("articletype");
      $articlecountry = $this->input->post("articlecountry");
      $articledorp = $this->input->post("articledorp");
      $articlehouse = $this->input->post("articlehouse");

      $session_data = $this->session->userdata('logged_in');
      $idUser = $session_data['id_usuario'];

      $config['upload_path'] = './assets/load-files/';
      $config['allowed_types'] = 'xls|xlsx|csv';
      $config['max_size'] = 10000;
      $config['encrypt_name'] = TRUE;
      print_r($_POST);
      echo $idUser;
      $this->load->library('upload', $config);

      if (!$this->upload->do_upload($file_element_name)) {
          echo json_encode(array("response" => "failed"));
      } else {

          $data = $this->upload->data();
          $filePath = $data['full_path'];
          $this->load->library('excel');
          $objPHPExcel = $this->excel=PHPExcel_IOFactory::load($filePath);
          $objPHPExcel = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

          $i=0;
          $lastID = 0;
          /*foreach ($objPHPExcel as $objCell) {
            if($i>2){
              $articleDetail = (object) array("IDType"=>$objCell['A'],"Serial"=>$objCell['B'],"Model"=>$objCell['C'],"Plate"=>$objCell['D'],"Features"=>$objCell['E'],
              "Comments"=>$objCell['F'],"DateIn"=>$objCell['G'],"DateExp"=>$objCell['H'],"Donation"=>$objCell['I']);

              $lastID = $this->inventory_model->savearticleunityfile($articleDetail->IDType, $articleDetail->Serial, $articleDetail->Model, $articleDetail->Plate, $articleDetail->Features, 
                $articleDetail->Comments, $articleDetail->DateIn, $articleDetail->DateExp, $articleDetail->Donation, $articletype, $articlecountry, $articledorp, $articlehouse, $idUser);
              if($lastID != 0) {
                if($articleDetail->Donation == 0){
                  $buyDetail = (object) array("IDArticle"=>$lastID,"TipoPago"=>$objCell['J'],"PaymentNum"=>$objCell['K'],"Provider"=>$objCell['L'],"InvoiceNum"=>$objCell['M']);
                  $result = $this->inventory_model->savearticlepayment($buyDetail->IDArticle, $buyDetail->TipoPago, $buyDetail->PaymentNum, $buyDetail->Provider, $buyDetail->InvoiceNum);
                  if(!$result) {
                    $lines = ((int) $i - 2);
                    echo json_encode(array("response" => "failed", "lines" => $lines));
                    exit();
                  }
                }
              } else {
                $lines = ((int) $i - 2);
                echo json_encode(array("response" => "failed", "lines" => $lines));
                exit();
              }
            }
            $lastID = 0;
            $i++;           
          }*/

          $lines = ((int) $i - 2);
          //echo json_encode(array("response" => "success", "lines" => $lines));
          //var_dump($myObjExcel);
          //query the database
          /*$result = $this->articles_model->savearticle($articleid, $articlename, $articletype, $articledescription, $articlefeatures, $articlecomments, $data['file_name']);
          if($result) {
            echo json_encode(array("response" => "success"));
          } else {
            echo json_encode(array("response" => "failed"));
          }*/

          @unlink($_FILES[$file_element_name]);
      }
      
  } else {
      echo json_encode(array("response" => "incomplete_form"));
  }
}

function returnallarticles(){
   $result = $this->articles_model->getallarticles();
   if($result) {
     echo json_encode($result);
   }
}

function returnarticlesstatus(){
   $result = $this->articles_model->getallarticlesstatus();
   if($result) {
     echo json_encode($result);
   }
}

}
?>